<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['movie:profession:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="Sort"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="professionList"
      row-key="professionId"
      :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column label="职业名称" header-align="center" align="left" prop="name" />
      <el-table-column label="英文名称" align="center" prop="nameEn" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" width="180">
        <template #default="scope">
          <el-button link type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['movie:profession:edit']">编辑</el-button>
          <el-button link type="primary" @click="handleAdd(scope.row)" v-hasPermi="['movie:profession:add']">新增</el-button>
          <el-button link type="primary" @click="handleDelete(scope.row)" v-hasPermi="['movie:profession:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改职业对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="professionRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="上级菜单">
          <el-tree-select
            v-model="form.parentId"
            :data="menuOptions"
            :props="{ value: 'professionId', label: 'name', children: 'children' }"
            value-key="professionId"
            placeholder="选择上级菜单"
            check-strictly
            :default-expanded-keys="defaultExpandedKeys"
          />
        </el-form-item>
        <el-form-item label="中文名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入职业名称" />
        </el-form-item>
        <el-form-item label="英文名称" prop="nameEn">
          <el-input v-model="form.nameEn" placeholder="请输入职业英文名称" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="状态" clearable style="width: 240px">
            <el-option
              v-for="dict in sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="简介" prop="brief">
          <el-input
            v-model="form.brief"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 5 }"
            maxlength="200"
            show-word-limit
            placeholder="职业简介"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Profession">
import { listProfession, getProfession, delProfession, addProfession, updateProfession } from "@/api/movie/profession";
import { getCurrentInstance, ref, reactive, toRefs, nextTick } from 'vue';

const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict('sys_normal_disable');

const professionList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const title = ref("");
const isExpandAll = ref(true);
const refreshTable = ref(true);
const menuOptions = ref([]);
const defaultExpandedKeys = ref([]);

const data = reactive({
  form: {},
  queryParams: {},
  rules: {
    name: [
      { required: true, message: "职业名称不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}

/** 查询职业列表 */
function getList() {
  loading.value = true;
  listProfession(queryParams.value).then(response => {
    professionList.value = proxy.handleTree(response.rows, "professionId");
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    professionId: null,
    parentId: null,
    name: null,
    nameEn: null,
    status: null,
    brief: null
  };
  proxy.resetForm("professionRef");
}

/** 新增按钮操作 */
function handleAdd(row) {
  reset();
  getTreeselect();
  open.value = true;
  if (row && row.professionId) {
    defaultExpandedKeys.value = [0].concat(row.mpath.split(".")).map(value => Number(value));
    form.value.parentId = row.professionId;
  } else {
    form.value.parentId = 0;
  }
  title.value = "添加职业";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  getTreeselect();
  const professionId = row.professionId;
  const item = row.professionId ? row : professionList.value.find(value => value.professionId === professionId);
  form.value = { ...item };
  if (item.mpath) {
    defaultExpandedKeys.value = item.mpath.split(".").map(value => Number(value));
  }
  open.value = true;
  title.value = "修改职业";
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["professionRef"].validate(valid => {
    if (valid) {
      if (form.value.professionId != null) {
        updateProfession(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addProfession(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const professionIds = row.professionId;
  proxy.$modal.confirm('是否确认删除职业编号为"' + professionIds + '"的数据项？').then(function() {
    return delProfession(professionIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 查询菜单下拉树结构 */
function getTreeselect() {
  menuOptions.value = [];
  listProfession().then(response => {
    const menu = { professionId: 0, name: "主类目", children: [] };
    menu.children = proxy.handleTree(response.rows, "professionId");
    menuOptions.value.push(menu);
  });
}

getList();
</script>